﻿Imports System.Globalization
Imports System.IO

Partial Public Class Total_Ship
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim lbPage As Label = CType(Master.FindControl("lbPage"), Label)
            lbPage.Text = "TOTAL SHIP'S "
            txtDate.Text = Date.Now.AddDays(-14).ToString("dd MMM yyyy", New CultureInfo("en-US"))
            txtDateTo.Text = Date.Now.AddDays(14).ToString("dd MMM yyyy", New CultureInfo("en-US"))
        End If
    End Sub

    Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        If (txtDate.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุเวลาเริ่มต้น!!');", True)
            Exit Sub
        End If
        If (txtDateTo.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุเวลาสิ้นสุด!!');", True)
            Exit Sub
        End If
        Dim service As New _ReportingService.Report

        Dim optWindow As String = "left=0,top=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no"
        Dim Orientation As Boolean = True
        Dim ReportName As String = String.Empty
        Dim reportDataSource As String = String.Empty
        Dim paraName As String = String.Empty
        Dim paraValue As String = String.Empty
        Dim reportType As String = String.Empty
        Dim urlDisplay As String = String.Empty
        Dim dsReport As New DataSet
        Dim dt As DataTable = clsGet.Get_Total_Ship(radioPORT.SelectedValue, txtDate.Text.Split(" ")(2).Trim() & CDate(txtDate.Text).ToString("MMdd"), txtDateTo.Text.Split(" ")(2).Trim() & CDate(txtDateTo.Text).ToString("MMdd"))
        If (dt.Rows.Count > 0) Then
            dsReport.Tables.Add(dt.Copy)
            dsReport.AcceptChanges()
            ReportName = "Total_Ship.rdlc"
            ' dsReport = New DataSet
            reportDataSource = "TotalSHIP_Value"
            paraName = "PORT;DateTimeNow"
            If (radioPORT.SelectedValue = "B") Then
                paraValue = "Bulk" & ";" & DateTime.Now.ToString()
            Else
                paraValue = "Liquid & Chemical" & ";" & DateTime.Now.ToString()
            End If

            Orientation = True
            reportType = radioType.SelectedValue

            urlDisplay = service.CreateReporting(dsReport, reportDataSource, reportType, ReportName, paraName, paraValue, Orientation, "")

            Dim oldFile As String = String.Empty
            oldFile = Server.MapPath(urlDisplay)
            Dim fileInfo As FileInfo = New FileInfo(oldFile)
            Dim reportPath As String = "../WriteReport/"

            Dim newFileName As String = DateTime.Now.ToString("_yyMMdd_HHmmss", New CultureInfo("en-US"))
            newFileName = "Total_Ships_Report" & newFileName

            If fileInfo.Exists Then
                fileInfo.MoveTo(fileInfo.DirectoryName & "/" & newFileName & fileInfo.Extension)
                urlDisplay = reportPath & newFileName & fileInfo.Extension
            End If

            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "window.open('" + urlDisplay + "','_blank','" + optWindow + "');", True)
        Else
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ไม่พบข้อมูล!!');", True)
        End If

    End Sub
    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "M")
        End If
    End Sub
End Class